package com.yfux.cling_player.ui;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.permissionx.guolindev.PermissionX;
import com.yfux.cling_player.databinding.ActivityMainBinding;
import com.yfux.cling_player.service.DLNARendererService;
import com.yfux.cling_player.util.LogUtil;
import com.yfux.cling_player.util.WifiUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private LogUtil logger = new LogUtil("MainActivity");

    @SuppressLint("SetTextI18n")
    private void resetWifiInfo(Context context) {
        binding.deviceName.setText("设备名称：不想投屏(o_O)");
        String ssid = WifiUtil.getWiFiName(context);
        if (ssid != null
                && !ssid.equals("")
                && !ssid.equals("<disabled>")
                && !ssid.equals("<not connect>")
                && !ssid.equals("<unknown>")
                && !ssid.equals("<permission denied>")) {
            binding.wifiName.setText("WLAN名称：" + ssid);
            binding.wifiName.setVisibility(View.VISIBLE);
        } else
            binding.wifiName.setVisibility(View.GONE);
        String ip = WifiUtil.getWiFiIpAddress(context);
        if (ip != null && !ip.equals("")) {
            binding.ip.setText("设备IP地址：" + ip);
            binding.ip.setVisibility(View.VISIBLE);
        } else {
            binding.ip.setVisibility(View.GONE);
        }
    }

    private void showExitConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setTitle("确认退出")
                .setMessage("您确定要退出应用吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish(); // 退出应用
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss(); // 关闭对话框
                    }
                })
                .create()
                .show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        logger.i("MainActivity.onCreate()");

        PermissionX.init(this)
                .permissions(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                )
                .request((boolean allGranted, List<String> grantedList, List<String> deniedList) -> {
                    if (allGranted) {
                        resetWifiInfo(this);
                    } else {
                        Toast.makeText(MainActivity.this, "Permission denied: "
                                + deniedList, Toast.LENGTH_SHORT).show();
                    }
                });

        DLNARendererService.startService(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean ret = super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showExitConfirmationDialog();
        }
        return ret;
    }
}